package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Eventpunish;
import cn.edu.scau.cmi.ema.domain.Punish;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("EventpunishController")
public class EventpunishController extends CmiEmaController {

	@RequestMapping("/listEventpunishs")
	public ModelAndView listEventpunishs(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = eventpunishPaginationService.updatePagination(request, pagination);
		Set<Eventpunish> eventpunishs = eventpunishPaginationService.getPaginationEventpunishs(pagination);

		mav.addObject("eventpunishs", eventpunishs);
		mav.addObject("entityName", "eventpunish");
		mav.addObject("pagination", pagination);
		mav.setViewName("eventpunish/listEventpunishs");
		return mav;
	}

	public String listEventpunishs() {
		return "redirect:/listEventpunishs";
	}

	@RequestMapping("/viewEventpunish")
	public ModelAndView viewEventpunish(@RequestParam Integer eventpunishId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("eventpunish", eventpunishDAO.findEventpunishByPrimaryKey(eventpunishId));
		mav.setViewName("eventpunish/viewEventpunish");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEventpunishs")
	public ModelAndView confirmDeleteEventpunishs(
			@RequestParam(value = "deleteEventpunishsIds", required = true) Set<Integer> deleteEventpunishsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Eventpunish> deleteEventpunishs = eventpunishDAO.findEventpunishsByIds(deleteEventpunishsIds);
		Set<Eventpunish> referencedEventpunishs = eventpunishService.loadReferenceEventpunishs(deleteEventpunishs);
		boolean hasReferenced;
		if (referencedEventpunishs.size() == 0) {
			mav.addObject("deleteEntitys", deleteEventpunishs);
			hasReferenced = false;
		} else {
			deleteEventpunishs.removeAll(referencedEventpunishs);
			mav.addObject("referencedEntitys", referencedEventpunishs);
			mav.addObject("deleteEntitys", deleteEventpunishs);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Eventpunish");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEventpunishs");
		return mav;
	}

	@RequestMapping("/deleteEventpunish")
	public String deleteEventpunish(@RequestParam Integer eventpunishId) {
		Eventpunish eventpunish = eventpunishDAO.findEventpunishByPrimaryKey(eventpunishId);
		eventpunishService.deleteEventpunish(eventpunish);
		return "forward:/listEventpunishs";
	}

	@RequestMapping("/confirmDeleteEventpunish")
	public ModelAndView confirmDeleteEventpunish(@RequestParam Integer eventpunishId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("eventpunish", eventpunishDAO.findEventpunishByPrimaryKey(eventpunishId));

		mav.setViewName("eventpunish/deleteEventpunish");

		return mav;
	}

	@RequestMapping("/deleteEventpunishs")
	public String deleteEventpunishs(
			@RequestParam(value = "deleteEventpunishsId", required = true) Set<Integer> deleteEventpunishsId) {
		eventpunishService.deleteEventpunishs(deleteEventpunishsId);
		return "forward:/listEventpunishs";
	}

	@RequestMapping("/editEventpunish")
	public ModelAndView editEventpunish(@RequestParam Integer eventpunishId) {

		ModelAndView mav = new ModelAndView();

		Eventpunish eventpunish = eventpunishDAO.findEventpunishByPrimaryKey(eventpunishId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("eventpunish", eventpunish);

		// 把所有的引用eventpunish对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventpunishService.loadEventpunishObjectReferencedSet(eventpunish));

		// 将编辑页面eventpunish添加到mav中
		mav.setViewName("eventpunish/editEventpunish");
		return mav;
	}

	@RequestMapping("/newEventpunish")
	public ModelAndView newEventpunish() {
		ModelAndView mav = new ModelAndView();
		Eventpunish eventpunish = new Eventpunish();

		mav.addObject("eventpunish", eventpunish);

		// 把所有的引用eventpunish对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventpunishService.loadEventpunishObjectReferencedSet(eventpunish));

		mav.addObject("newFlag", true);
		mav.setViewName("eventpunish/editEventpunish");

		return mav;
	}

	@RequestMapping("/saveEventpunish")
	public ModelAndView saveEventpunish(@ModelAttribute Eventpunish eventpunish) {

		ModelAndView mav = new ModelAndView();

		Event eventTemp = eventpunish.getEvent();
		Event event = eventTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(eventDAO.findEventById(eventTemp.getId()));
		Punish punishTemp = eventpunish.getPunish();
		Punish punish = punishTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(punishDAO.findPunishById(punishTemp.getId()));

		Eventpunish existingEventpunish = CmiHibernateProxyUtil.deproxy(eventpunishDAO
				.findEventpunishByPhysicalPrimaryKey(event.getId(), punish.getId(), eventpunish.getDate()));

		eventpunish.setEvent(event);
		eventpunish.setPunish(punish);
		// 新建记录
		if (eventpunish.getId() == null) {
			if (eventpunish.equals(existingEventpunish)) {

				mav.addAllObjects(eventpunishService.loadEventpunishClassReferencedSet(eventpunish));
				// mav = editContinue(mav, eventpunish);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventpunish/editEventpunish");
			} else {
				eventpunishService.saveEventpunish(eventpunish);
				mav.setViewName("forward:/listEventpunishs");
			}
		}
		// 编辑记录
		else {
			Eventpunish originEventpunish = CmiHibernateProxyUtil
					.deproxy(eventpunishDAO.findEventpunishByPrimaryKey(eventpunish.getId()));
			if (!eventpunish.equals(originEventpunish) && eventpunish.equals(existingEventpunish)) {
				mav.addAllObjects(eventpunishService.loadEventpunishClassReferencedSet(eventpunish));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventpunish/editEventpunish");
			} else {
				eventpunishService.saveEventpunish(eventpunish);
				mav.setViewName("forward:/listEventpunishs");
			}
		}
		return mav;
	}

	@RequestMapping("/eventpunishController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
